home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / zm16src.lzh / TYME.C < prev    next >
C/C++ Source or Header  |  1988-05-19  |  4KB  |  208 lines

  1. /*
  2.  *     Time conversions Module
  3.  *
  4.  *        Jwahar Bammi
  5.  *            usenet: mandrill!bammi@{decvax,sun}.UUCP
  6.  *            csnet:  bammi@mandrill.ces.CWRU.edu
  7.  *            arpa:   bammi@mandrill.ces.CWRU.edu
  8.  *            CompuServe: 71515,155
  9.  */
  10.  
  11. #include "config.h"
  12. #include <stdio.h>
  13.  
  14.  
  15. /*
  16.  * days in a given year
  17.  */
  18. #define days_in_year(Y) (leap(Y) ? 366 : 365)
  19.  
  20. /* # of days / month in a normal year */
  21. static unsigned int md[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  22.  
  23. static int leap (y)
  24. int y;
  25. {
  26.     y += 1900;
  27.     if ((y % 400) == 0)
  28.         return (1);
  29.     if ((y % 100) == 0)
  30.         return (0);
  31.     return ((y % 4) == 0);
  32. }
  33.  
  34. /* Return the number of days between Jan 1, Given Year and the given
  35.  * broken-down time.
  36.  */
  37.  
  38. static unsigned long ndays (since, year, month, day)
  39. unsigned int year, month, day;
  40. {
  41.     register unsigned long n = day;
  42.     register unsigned int m, y;
  43.     
  44.     for (y = since; y < year; y++)
  45.     {
  46.         n += 365;
  47.         if (leap (y)) n++;
  48.     }
  49.     for (m = 0; m < (month-1); m++)
  50.         n += md[m] + ( ((m == 1) && leap(y))? 1 : 0);
  51.  
  52.     return (n);
  53. }
  54.  
  55. /* Convert a broken-down time into seconds
  56.  *
  57.  */
  58.  
  59. unsigned long tm_to_time (base_year, year, month, day, hours, mins, secs)
  60. unsigned int base_year, year, month, day, hours, mins, secs;
  61. {
  62.     register unsigned long t;
  63.     extern unsigned long ndays();
  64.     
  65.     t = (ndays(base_year, year, month, day) - 1L) * (unsigned long)86400L
  66.         + hours * (unsigned long)3600L + mins * (unsigned long)60L + secs;
  67.  
  68.     return t;
  69. }
  70.  
  71. /*
  72.  * Convert ST time to Unix time
  73.  *
  74.  */
  75. unsigned long st2unix(time, date)
  76. unsigned int time, date;
  77. {
  78.     extern unsigned long tm_to_time();
  79.     
  80.     unsigned int yr = ((date >> 9) & 0x007f) + 80;  /* dissect the date */
  81.     unsigned int mo = (date >> 5) & 0x000f;
  82.     unsigned int dy = date & 0x1f;
  83.     
  84.     unsigned int hr = (time >> 11) & 0x001f;        /* dissect the time */
  85.     unsigned int mm = (time >> 5)  & 0x003f;
  86.     unsigned int ss = (time & 0x001f) * 2;
  87.  
  88. #ifdef SDEBUG
  89.     printf("%d/%d/%d  %d:%d:%d\n", mo, dy, yr,hr, mm, ss);
  90. #endif
  91.     return (tm_to_time(70, yr, mo, dy, hr, mm, ss) -
  92.         (unsigned long)GMTDIFF);
  93. }
  94.  
  95. /*
  96.  * Convert Unix Time to ST Time
  97.  *
  98.  */
  99. void unix2st(Unix, time, date)
  100. unsigned long Unix;
  101. unsigned int *date, *time;
  102. {
  103.     long stbase;
  104.     unsigned int hours, yrs, day, months, mins, seconds, t;
  105.     long days, secs;
  106.     extern unsigned long tm_to_time();
  107.  
  108. #ifdef DEBUG
  109. printf("\n\nUnix Time %ld\n", Unix);
  110. #endif
  111.  
  112.     
  113.     if((Unix - tm_to_time(70, 80, 0, 0, 0, 0, 0)) <= 0)  /* base 1980 */
  114.     {
  115.         /* thats before St's time */
  116.         *time = 0;
  117.         *date = (1 << 5) | 1;    /* Jan 1, 1980 00:00:00 GMT */
  118. #ifdef DEBUG
  119. printf("Before my time\n");
  120. #endif
  121.         return;
  122.     }
  123.  
  124.     stbase = Unix;    /* do from base year 1970 */
  125.  
  126.     days = stbase / 86400L;    /* 3600*24 */
  127.     secs = stbase % 86400L + GMTDIFF;
  128.     if(secs < 0)    /* previous day here */
  129.     {
  130.         days -= 1;
  131.         secs += 86400L;
  132.     }
  133.  
  134.     /* extract hrs : mins : seconds */
  135.     hours = secs / 3600;
  136.     secs = secs - (hours * 3600);
  137.     mins = secs / 60;
  138.     seconds = secs - (mins * 60);
  139.     seconds &= ~1L;            /* ST has 2 sec resolution */
  140.  
  141.     /* get the year and day of the year */
  142.     for(t = 70; days >= days_in_year(t); t++)
  143.         days -= days_in_year(t);
  144.     yrs = t;
  145.     day = days;
  146.  
  147.     /* get the month */
  148.     if(days_in_year(yrs) == 366)
  149.         md[1] = 29;
  150.  
  151.     for(t = 0; day >= md[t]; t++)
  152.         day -= md[t];
  153.  
  154.     md[1] = 28;        
  155.     day = day + 1;
  156.     months = t + 1;
  157.  
  158. #ifdef DEBUG
  159. printf("%d/%d/%d   %d:%d:%d\n", months, day, yrs, hours, mins, seconds);
  160. #endif
  161.  
  162.     yrs -= 80;
  163.     *date = (((yrs & 0x007f) << 9) | ((months & 0x000f) << 5)
  164.           | (day & 0x001f));
  165.     
  166.     *time = (((hours & 0x001f) << 11) | ((mins & 0x003f) << 5)
  167.           | (seconds & 0x001e));
  168. }
  169.  
  170. #ifdef TEST
  171. #include <stdio.h>
  172. #include <osbind.h>
  173.  
  174. main()
  175. {
  176.     unsigned int time, date;
  177.     unsigned long Unix;
  178.     extern unsigned long st2unix();
  179.     
  180.     time = Tgettime();
  181.     date = Tgetdate();
  182.     Unix = st2unix(time, date);
  183.  
  184.     printd(time, date);
  185.     printf("Unix Time %ld\n", Unix);
  186.  
  187.     unix2st(Unix, &time, &date);
  188.     printd(time, date);
  189. }
  190.  
  191. printd(time, date)
  192. unsigned int time, date;
  193. {
  194.     
  195.     unsigned int yr = ((date >> 9) & 0x007f) + 80;  /* dissect the date */
  196.     unsigned int mo = (date >> 5) & 0x000f;
  197.     unsigned int dy = date & 0x1f;
  198.     
  199.     unsigned int hr = (time >> 11) & 0x001f;        /* dissect the time */
  200.     unsigned int mm = (time >> 5)  & 0x003f;
  201.     unsigned int ss = (time & 0x001f) * 2;
  202.  
  203.     printf("%d/%d/%d\t%d:%d:%d\n", mo, dy, yr, hr, mm, ss);
  204. }
  205. #endif /* TEST */
  206.  
  207. /* -eof- */
  208.